'use strict';

/**
 * page service
 */
angular.module('sfhtHaitaoCmsApp')
    .service('UserService', function($q, Gateway, $injector) {
        // 通用的request方法返回promise对象
        var request = function(api, params, isForceUserLogin) {
            var deferred = $q.defer();
            var api = $injector.get(api);
            var gateway = new Gateway(api, params);
            gateway.sendRequest(false, false)
                .then(function(data) {
                    deferred.resolve(data);
                }, function(data) {
                    deferred.reject(data);
                });
            return deferred.promise;
        };

        /**
         * 将所有接口做封装，便于接口管控
         */
        return {
            'logout': function() {
                var params = {}
                return request('sf_b2c_mall_api_cms_logout', params, false);
            },

            'login': function(params) {
                return request('sf_b2c_mall_api_cms_webLogin', params, false);
            },

            'changePassword': function(params){
                return request('sf_b2c_mall_api_cms_changePassword', params, false);
            }
        };
    });
